<!DOCTYPE html>
<html>
<head>
    <title>编辑订阅</title>
    {% include 'system/common/header.html' %}
</head>
<body>
    <form class="layui-form" action="">
        <div class="mainBox">
            <div class="main-container">
                <div class="layui-form-item">
                    <label class="layui-form-label">订阅名称</label>
                    <div class="layui-input-block">
                        <input type="hidden" name="id" value="{{ rss.id }}">
                        <input type="text" name="name" value="{{ rss.name }}" lay-verify="required" 
                               placeholder="请输入订阅名称" autocomplete="off" class="layui-input">
                    </div>
                </div>

                <div class="layui-form-item">
                    <label class="layui-form-label">订阅类型</label>
                    <div class="layui-input-block">
                        <select name="type" lay-verify="required">
                            <option value="txt" {{ 'selected' if rss.type == 'txt' }}>TXT</option>
                            <option value="json" {{ 'selected' if rss.type == 'json' }}>JSON</option>
                            <option value="m3u" {{ 'selected' if rss.type == 'm3u' }}>M3U</option>
                            <option value="genre" {{ 'selected' if rss.type == 'genre' }}>GENRE-TXT</option>
                        </select>
                    </div>
                </div>

                <div class="layui-form-item">
                    <label class="layui-form-label">订阅地址</label>
                    <div class="layui-input-block">
                        <input type="text" name="url" value="{{ rss.url }}" lay-verify="required" 
                               placeholder="请输入订阅地址" autocomplete="off" class="layui-input" >
                        <button type="button" class="pear-btn pear-btn-primary pear-btn-sm" id="checkUrl" style="margin-top:5px;">
                            <i class="layui-icon">&#xe615;</i> 检查地址
                        </button>
                    </div>
                </div>


                <div class="layui-form-item">
                    <label class="layui-form-label">排序</label>
                    <div class="layui-input-block">
                        <input type="number" name="sort" value="{{ rss.sort }}" lay-verify="number" 
                               class="layui-input">
                    </div>
                </div>

                <div class="layui-form-item">
                    <label class="layui-form-label">状态</label>
                    <div class="layui-input-block">
                        <input type="checkbox" name="enable" value="{{ rss.enable|int }}" lay-skin="switch" 
                               lay-text="启用|禁用" lay-filter="enableFilter" {{ 'checked' if rss.enable }}>
                    </div>
                </div>

                <div class="layui-form-item">
                    <label class="layui-form-label">加密</label>
                    <div class="layui-input-block">
                        <input type="checkbox" name="encrypt" value="{{ rss.encrypt|int }}" lay-skin="switch" 
                               lay-text="是|否" lay-filter="encryptFilter" {{ 'checked' if rss.encrypt }}>
                    </div>
                </div>

                <div class="layui-form-item layui-form-text">
                    <label class="layui-form-label">备注</label>
                    <div class="layui-input-block">
                        <textarea name="remark" placeholder="请输入备注信息" 
                                  class="layui-textarea">{{ rss.remark }}</textarea>
                    </div>
                </div>
            </div>
        </div>
        <div class="bottom">
            <div class="button-container">
                <button type="submit" class="pear-btn pear-btn-primary pear-btn-sm" lay-submit lay-filter="rss-update">
                    <i class="layui-icon layui-icon-ok"></i>
                    提交
                </button>
                <button type="reset" class="pear-btn pear-btn-sm">
                    <i class="layui-icon layui-icon-refresh"></i>
                    重置
                </button>
            </div>
        </div>
    </form>
    {% include 'system/common/footer.html' %}   
    <script>
        layui.use(['form', 'jquery'], function(){
            let form = layui.form;
            let $ = layui.jquery;
            
            // 监听开关切换
            form.on('switch(enableFilter)', function(data){
                let enableInput = $('input[name="enable"]');
                if(data.elem.checked) {
                    enableInput.val(1);
                } else {
                    enableInput.val(0);
                }
                //console.log('当前状态值：', enableInput.val()); // 调试用
            });
            // 监听加密开关切换
            form.on('switch(encryptFilter)', function(data){
                let encryptInput = $('input[name="encrypt"]');
                if(data.elem.checked) {
                    encryptInput.val(1);
                } else {
                    encryptInput.val(0);
                }
            });
        });
    </script>
    <script>
        layui.use(['form', 'jquery'], function() {
            let form = layui.form;
            let $ = layui.jquery;

            // URL检查
            $('#checkUrl').click(function() {
                    let url = $('input[name="url"]').val();
                    if (!url) {
                        layer.msg('请输入订阅地址', {icon: 2});
                        return;
                    }
                    
                    let loading = layer.load();
                    $.ajax({
                        url: '/iptvadmin/rss/check',
                        type: 'POST',
                        contentType: 'application/json',
                        data: JSON.stringify({url: url}),
                        success: function(result) {
                            layer.close(loading);
                            if (result.success) {
                                layer.msg('订阅地址可访问', {icon: 1});
                            } else {
                                layer.msg(result.msg, {icon: 2});
                            }
                        },
                        error: function(xhr, status, error) {
                            layer.close(loading);
                            console.error('检查失败:', error);
                            layer.msg('检查失败：' + error, {
                                icon: 2,
                                time: 2000
                            });
                        }
                    });
                });

            // 表单提交
            form.on('submit(rss-update)', function(data) {
                // 表单验证
                if (!data.field.name) {
                    layer.msg('请输入订阅名称', {icon: 2});
                    return false;
                }
                if (!data.field.url) {
                    layer.msg('请输入订阅地址', {icon: 2});
                    return false;
                }
                
                let loading = layer.load();
                $.ajax({
                    url: '/iptvadmin/rss/update',
                    data: JSON.stringify(data.field),
                    dataType: 'json',
                    contentType: 'application/json',
                    type: 'post',
                    success: function(result) {
                        layer.close(loading);
                        if (result.success) {
                            layer.msg(result.msg, {
                                icon: 1,
                                time: 1000
                            }, function() {
                                parent.layer.close(parent.layer.getFrameIndex(window.name));
                                parent.layui.table.reload('rss-table');
                            });
                        } else {
                            layer.msg(result.msg, {
                                icon: 2,
                                time: 1000
                            });
                        }
                    },
                    error: function(xhr, status, error) {
                        layer.close(loading);
                        console.error('更新失败:', error);
                        layer.msg('更新失败：' + error, {
                            icon: 2,
                            time: 2000
                        });
                    }
                });
                return false;
            });
        });
    </script>
</body>
</html>